package com.example.neusoft215gdh.androidpro_21.Activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;

import com.example.neusoft215gdh.androidpro_21.Adapter.OrderAdapter;
import com.example.neusoft215gdh.androidpro_21.JavaBean.Order;
import com.example.neusoft215gdh.androidpro_21.R;
import com.example.neusoft215gdh.androidpro_21.Service.OrderSeverice;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Rainbow on 2017/11/12.
 */

public class OrderActivity extends Activity{
    private RecyclerView recyclerView;
    private OrderAdapter adapter;
    private ImageButton back;
    String userid;
    String sessionid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);

        recyclerView = (RecyclerView)findViewById(R.id.recyclerView_order);
        back= (ImageButton) findViewById(R.id.order_back);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175/workout/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        OrderSeverice orderSeverice =retrofit.create(OrderSeverice.class);

        //SharedPreferences sharedPreferences=getSharedPreferences("Userid", Context.MODE_PRIVATE);
        SharedPreferences user=getSharedPreferences("Userid",0);
        //userid=user.getString("user_id","");
       // sessionid=user.getString("sessionId","");
        Call< List<Order> > listCall = orderSeverice.orderList(1,0,1,"5nhsgpf67jh52nqsn90s2qptc7");
        listCall.enqueue(new Callback<List<Order>>() {
            @Override
            public void onResponse(Call<List<Order>> call, Response<List<Order>> response) {

                List<Order> orderList = response.body();

                if (OrderActivity.this != null) {
                    adapter = new OrderAdapter(OrderActivity.this, orderList);
                    LinearLayoutManager layoutManager = new LinearLayoutManager(OrderActivity.this);
                    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    recyclerView.setLayoutManager(layoutManager);
                    recyclerView.setAdapter(adapter);
                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                }

            }

            @Override
            public void onFailure(Call<List<Order>> call, Throwable t) {

            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
}
